package com.ming.controller;

import com.ming.service.FileService;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.PublicKey;
import java.util.List;

/**
 * @Author:weiming
 * @Date:2020/12/1
 */
@RestController
public class FileController {
    @Resource
    FileService fileService;

    @PostMapping("/upload")
    public String upload(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException {
        return fileService.uploadFile(file);
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/multiupload")
    public String multiupload(HttpServletRequest request) throws IOException {
        List<MultipartFile> fileList = ((MultipartHttpServletRequest) request).getFiles("files");;
        return fileService.multiUploadFile(fileList);
    }

    @GetMapping("/download")
    public  String download(HttpServletResponse response) throws UnsupportedEncodingException {
        return  fileService.download(response);
    }

    /**
     * Minio对象文件上传
     * @param file
     * @return
     */
    @PostMapping("/minio/upload")
    public String minioUpload(@RequestParam("file") MultipartFile file){
        return "<img src='"+fileService.minioUpload(file)+"'/>";
    }


}
